﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Comod_StorageAdd : System.Web.UI.Page
{
    ZSql zs = new ZSql();
    Function f = new Function();
    ZSql cmid = new ZSql();
    ZSql ms = new ZSql();
    ZSql info = new ZSql();//添加修改
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblName.Text = Server.UrlDecode(Request.Cookies["user"].Values["name"]);//操作人
            lblPatd.Text = f.getModuleidAdd(Request.QueryString["moduid"]);//当前位置
            hidModuid.Value = Request.QueryString["moduid"];
            ddlComodName();
            ddlSupelier();
            ddlComoType();
            initUnit();
            if (!string.IsNullOrEmpty(Request["id"]))//修改操作
            {
                initData();
            }
            else
            {
                txtSdate.Text = DateTime.Now.ToString();//初始化录入时间
                lblDate.Text = DateTime.Now.ToShortDateString();//初始化操作时间
            }
        }
        if (Request.QueryString["action"].ToString() != "flag")//商品入库或者修改时，隐藏审核内容
        {
            ClientScript.RegisterStartupScript(typeof(Page), "javaScript", "dispAdd()", true);
        }
    }
    protected void ddlComodName()
    {
        ZSql ware = new ZSql();
        ware.Open("select warehouseid,warehousename from CS_Warehouse");
        if (ware.m_table.Rows.Count > 0)
        {
            ware.Open("select warehouseid,warehousename from CS_Warehouse where AuditState='已通过'");
            if (ware.m_table.Rows.Count > 0)
            {
                ddlComodiName.DataSource = ware.m_table;
                ddlComodiName.DataTextField = "warehousename";
                ddlComodiName.DataValueField = "warehouseid";
                ddlComodiName.DataBind();
                ddlComodiName.Items.Insert(0, new ListItem("选择仓库", ""));

                ware.Close();
            }
            else
            {
                Response.Write("<script>alert('仓库未审核！');</script>");
                ddlComodiName.Items.Insert(0, new ListItem("选择仓库", ""));
            }
        }
        else
        {
            Response.Write("<script>alert('仓库为空请添加仓库！');</script>");
            ddlComodiName.Items.Insert(0, new ListItem("选择仓库", ""));
        }


    }

    private void ddlSupelier()
    {
        zs.Open("select SupplierId,Supplier from CS_Supplier");
        if (zs.m_table.Rows.Count > 0)
        {
            zs.Open("select SupplierId,Supplier from CS_Supplier where AuditState='已通过'");
            if (zs.m_table.Rows.Count > 0)
            {
                ddlSupplier.DataSource = zs.m_table;
                ddlSupplier.DataTextField = "Supplier";
                ddlSupplier.DataValueField = "SupplierId";
                ddlSupplier.DataBind();
                ddlSupplier.Items.Insert(0, new ListItem("选择供货商", ""));
                zs.Close();
            }
            else
            {
                Response.Write("<script>alert('供货商未审核！');</script>");
                ddlSupplier.Items.Insert(0, new ListItem("选择供货商", ""));
            }
        }
        else
        {
            Response.Write("<script>alert('供货商为空请添加供货商！');</script>");
            ddlSupplier.Items.Insert(0, new ListItem("选择供货商", ""));
        }
    }

    private void ddlComoType()
    {

        ZSql com = new ZSql();
        com.Open("select TypeId,ComoditiesType from ComoditiesType where AuditState=1");
        ddlType.DataSource = com.m_table;
        ddlType.DataTextField = "ComoditiesType";
        ddlType.DataValueField = "TypeId";
        ddlType.DataBind();
        ddlType.Items.Insert(0, new ListItem("选择商品类型", ""));
        ddlName.Items.Insert(0, new ListItem("选择商品品牌", ""));
        ddlSpecifications.Items.Insert(0, new ListItem("选择商品名称", ""));
        ddlUnits.Items.Insert(0, new ListItem("选择商品型号", ""));

        com.Close();

    }


    private void initUnit()
    {
        ZSql zUnit = new ZSql();

        zUnit.Open("select * from Units where AuditState=1");
        ddlck.DataSource = zUnit.m_table;
        ddlck.DataTextField = "Units";
        ddlck.DataValueField = "UnitsId";
        ddlck.DataBind();
        ddlck.Items.Insert(0, new ListItem("选择计量单位", ""));
        zUnit.Close();
    }

    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ZSql brand = new ZSql();
        string where = " 1=0 ";
        if (!string.IsNullOrEmpty(ddlType.SelectedValue)) where="TypeId="+ddlType.SelectedValue;
        brand.Open("select BrandId,Brand from Brand where " + where+ " and AuditState=1");
        ddlName.DataSource = brand.m_table;
        ddlName.DataTextField = "Brand";
        ddlName.DataValueField = "BrandId";
        ddlName.DataBind();
        ddlName.Items.Insert(0, new ListItem("选择商品品牌", ""));
        brand.Close();

    }

    protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
    {

        ZSql name = new ZSql();
        string where = " 1=0 ";
        if (!string.IsNullOrEmpty(ddlName.SelectedValue)) where = "BrandId=" + ddlName.SelectedValue;
        name.Open("select ComoditiesNameId,ComoditiesName from ComoditiesName where " + where + "and AuditState=1");
        ddlSpecifications.DataSource = name.m_table;
        ddlSpecifications.DataTextField = "ComoditiesName";
        ddlSpecifications.DataValueField = "ComoditiesNameId";
        ddlSpecifications.DataBind();
        ddlSpecifications.Items.Insert(0, new ListItem("选择商品名称", ""));
        name.Close();

    }


    protected void ddlSpecifications_SelectedIndexChanged(object sender, EventArgs e)
    {

        ZSql name = new ZSql();
        string where = " 1=0 ";
        if (!string.IsNullOrEmpty(ddlSpecifications.SelectedValue)) where = "ComoditiesNameId=" + ddlSpecifications.SelectedValue;
        name.Open("select SpecificationsId,Specifications from Specifications where " + where);

        ddlUnits.DataSource = name.m_table;
        ddlUnits.DataTextField = "Specifications";
        ddlUnits.DataValueField = "SpecificationsId";
        ddlUnits.DataBind();
        ddlUnits.Items.Insert(0, new ListItem("选择商品型号", ""));
        name.Close();
    }


    protected void btnAdd_Click(object sender, EventArgs e)//系统提交
    {
        double ProcMoney=0, SuppliMoney=0, newSuppliMoney=0, RetailMoney=0, WholeMoney1=0, WholeMoney2=0, RestMoney=0, ChainMoney=0, Prices=0, ActiveMoney=0;
        ZSql zs = new ZSql();
        zs.Open("select top 1 * from CS_Stock where ComodBarcode ='" + txtCode.Text + "' order by stockId desc");
        if (zs.m_table.Rows.Count > 0)
        {
            ProcMoney = Convert.ToDouble(zs.m_table.Rows[0]["ProcMoney"]);
            SuppliMoney = Convert.ToDouble(zs.m_table.Rows[0]["SuppliMoney"]);
            newSuppliMoney = Convert.ToDouble(zs.m_table.Rows[0]["newSuppliMoney"]);
            RetailMoney = Convert.ToDouble(zs.m_table.Rows[0]["RetailMoney"]);
            WholeMoney1 = Convert.ToDouble(zs.m_table.Rows[0]["WholeMoney1"]);
            WholeMoney2 = Convert.ToDouble(zs.m_table.Rows[0]["WholeMoney2"]);
            RestMoney = Convert.ToDouble(zs.m_table.Rows[0]["RestMoney"]);
            ChainMoney = Convert.ToDouble(zs.m_table.Rows[0]["ChainMoney"]);
            Prices = Convert.ToDouble(zs.m_table.Rows[0]["Prices"]);
            ActiveMoney = Convert.ToDouble(zs.m_table.Rows[0]["ActiveMoney"]);
        }
        zs.Close();


        string action = Request["action"];
        string sql = "";
        if (action == "add")//新增
            sql = "select top 1 * from CS_Stock";
        else//修改
            sql = "select top 1 * from CS_Stock where stockId=" + Request["id"];

        info.Open(sql);
        info.NextRow();
        if (action == "add")//新增
        {
            info.NewRow();
            //--------------价格------------------------------
            info["SuppliMoney"] = SuppliMoney;
            info["newSuppliMoney"] = newSuppliMoney;
            info["RetailMoney"] = RetailMoney;
            info["WholeMoney1"] = WholeMoney1;
            info["WholeMoney2"] = WholeMoney2;
            info["RestMoney"] = RestMoney;
            info["ChainMoney"] = ChainMoney;
            info["Prices"] = Prices;
            info["ActiveMoney"] = ActiveMoney;
        }
        info["ProcMoney"] = txtProcMoney.Text;//采购价
        info["ComodBarcode"] = txtCode.Text;//条码
        info["WarehouseId"] = ddlComodiName.SelectedValue;//仓库ID
        info["ComoditiesType"] = ddlType.SelectedValue;//类型ID
        info["SupplierId"] = ddlSupplier.SelectedValue;//供应商
        info["BrandId"] = ddlName.SelectedValue;//品牌ID
        info["ComoditiesName"] = ddlSpecifications.SelectedValue;//名称ID
        info["ModelId"] = ddlUnits.SelectedValue;//型号ID
        info["UnitId"] = ddlck.SelectedValue;//单位ID
        info["typeStockId"] = ddlTypeStocck.SelectedValue;//入库类型Id
        info["typeStock"] = ddlTypeStocck.SelectedItem.Text;//入库类型
        info["ProcNum"] = txtNum.Value;//进库数量
        info["Content"] = txtComoContent.Text;//备注
        info["storageTime"] = txtSdate.Text;//入库时间

        if (action != "flag")//不是审核
        {
            info["AuditState"] = "未审核";
            info["EditUserName"] = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);//编辑人
            info["EditDate"] = DateTime.Now.ToString();//时间
        }
        else
        {//审核
            if (flag1.Checked) info["AuditState"] = "已通过"; else info["AuditState"] = "未通过";
            info["AuditUserName"] = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);//编辑人
            info["AuditDate"] = DateTime.Now.ToString();//时间
        }

        info.Update();
        info.Close();
        Response.Write("<script>alert('操作已成功，请返回！');</script>");//返回页面
        Response.Write("<script>window.location='StorageManage.aspx?moduid=" + hidModuid.Value + "';</script>");//返回页面


    }

    private void initData()//当修改时,初使化数据
    {
        ZSql zs = new ZSql();
        zs.Open("select * from CS_Stock where StockId=" + Request["id"]);
        ddlComodiName.SelectedValue = zs.m_table.Rows[0]["WarehouseId"].ToString();
        ddlSupplier.SelectedValue = zs.m_table.Rows[0]["SupplierId"].ToString();
        ddlType.SelectedValue = zs.m_table.Rows[0]["ComoditiesType"].ToString();
        initBrand(zs.m_table.Rows[0]["ComoditiesType"].ToString());
        ddlTypeStocck.SelectedValue = zs.m_table.Rows[0]["typeStockId"].ToString();
        ddlName.SelectedValue = zs.m_table.Rows[0]["BrandId"].ToString();
        initComoName(zs.m_table.Rows[0]["BrandId"].ToString());
        ddlSpecifications.SelectedValue = zs.m_table.Rows[0]["ComoditiesName"].ToString();
        initUnits(zs.m_table.Rows[0]["ComoditiesName"].ToString());
        ddlUnits.SelectedValue = zs.m_table.Rows[0]["ModelId"].ToString();
        ddlck.SelectedValue = zs.m_table.Rows[0]["UnitId"].ToString();
        txtSdate.Text = zs.m_table.Rows[0]["storageTime"].ToString();
        txtCode.Text = zs.m_table.Rows[0]["ComodBarcode"].ToString();
        txtNum.Value = zs.m_table.Rows[0]["ProcNum"].ToString();
        txtProcMoney.Text = zs.m_table.Rows[0]["ProcMoney"].ToString();
        txtComoContent.Text = zs.m_table.Rows[0]["Content"].ToString();
        lblName.Text = zs.m_table.Rows[0]["EditUserName"].ToString();
        lblDate.Text = zs.m_table.Rows[0]["EditDate"].ToString();
        zs.Close();
    }
    private void initBrand(string id)
    {
        ZSql brand = new ZSql();
        brand.Open("select BrandId,Brand from Brand where TypeId=" + id);
        ddlName.DataSource = brand.m_table;
        ddlName.DataTextField = "Brand";
        ddlName.DataValueField = "BrandId";
        ddlName.DataBind();
        ddlName.Items.Insert(0, new ListItem("选择商品品牌", ""));
        brand.Close();
    }

    private void initComoName(string id)
    {
        ZSql name = new ZSql();
        name.Open("select ComoditiesNameId,ComoditiesName from ComoditiesName where BrandId=" + id);
        ddlSpecifications.DataSource = name.m_table;
        ddlSpecifications.DataTextField = "ComoditiesName";
        ddlSpecifications.DataValueField = "ComoditiesNameId";
        ddlSpecifications.DataBind();
        ddlSpecifications.Items.Insert(0, new ListItem("选择商品名称", ""));

        name.Close();
    }

    private void initUnits(string id)
    {
        ZSql name = new ZSql();
        name.Open("select SpecificationsId,Specifications from Specifications where ComoditiesNameId=" + id);

        ddlUnits.DataSource = name.m_table;
        ddlUnits.DataTextField = "Specifications";
        ddlUnits.DataValueField = "SpecificationsId";
        ddlUnits.DataBind();
        ddlUnits.Items.Insert(0, new ListItem("选择商品型号", ""));
        name.Close();
    }
    protected void ddlUnits_SelectedIndexChanged(object sender, EventArgs e)//型号编号
    {
        ZSql zs = new ZSql();
        string where = " 1=0 ";
        if (!string.IsNullOrEmpty(ddlUnits.SelectedValue)) where = "SpecificationsId=" + ddlUnits.SelectedValue;
        zs.Open("select * from Specifications where " + where);
        if (zs.Rows.Count > 0)
        {
            txtCode.Text = zs.m_table.Rows[0]["modulbh"].ToString();
            ddlck.SelectedValue = zs.m_table.Rows[0]["Units"].ToString();
        }
        else
        {
            txtCode.Text = "";
        }
        zs.Close();
    }
}
